//package com.origin.learn.flume.service.impl;
//
//import com.origin.learn.flume.annotation.FlumeComponentType;
//import com.origin.learn.flume.bean.*;
//import com.origin.learn.flume.service.Agent;
//import com.origin.learn.flume.util.ReflectUtil;
//import lombok.Setter;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.zookeeper.common.StringUtils;
//
//import java.lang.reflect.Field;
//import java.util.List;
//import java.util.Properties;
//
///**
// * @Description:
// * @Date: 2020/1/17
// * @Time: 11:12
// * @Created by:   dingtao
// * @Modified by:
// * @Modified at:
// */
//@Slf4j
//public class ReflectAgentPropertiesGenerator extends AbstractAgent implements Agent {
//
//
//    private FlumeAgent agent;
//    private Properties properties;
//
//    public ReflectAgentPropertiesGenerator() {
//        properties = new Properties();
//    }
//
//    public ReflectAgentPropertiesGenerator agent(FlumeAgent agent) {
//        this.agent = agent;
//        return this;
//    }
//
//    @Override
//    public boolean generateComponentProperties(FlumeComponent component) {
//        String componentType = getFlumeComponentType(component);
//
//        Class componentClass = component.getClass();
//        Class superClass = componentClass.getSuperclass();
//        try {
//            while (superClass != FlumeComponent.class) {
//                Field[] fields = superClass.getDeclaredFields();
//                addProperties(component, fields, componentType);
//                superClass = superClass.getSuperclass();
//            }
//            Field[] fields = componentClass.getDeclaredFields();
//            addProperties(component, fields, componentType);
//        } catch (Exception e) {
//            e.printStackTrace();
//            log.error(e.getLocalizedMessage());
//        }
//
//        return false;
//    }
//
//    private void addProperties(FlumeComponent component, Field[] fields, String componentType) throws IllegalAccessException, NoSuchFieldException {
//
//        StringBuilder stringBuilder = new StringBuilder();
//        String name = component.name();
//        for (Field field : fields) {
//            if (ReflectUtil.isPrimitiveAndItsPackagingType(field)) {
//                if ("name".equals(field.getName())) {
//                    continue;
//                }
//                field.setAccessible(true);
//                String propertyName = stringBuilder.append(agent.agentName()).append(".").append(name).append(".")
//                        .append(field.getName()).toString();
//                stringBuilder.delete(0, stringBuilder.length());
//                properties.put(propertyName, field.get(component));
//            } else if (field.getType().equals(List.class)) {
//                String propertyName;
//                if (component.getClass().equals(FlumeSource.class)) {
//                    propertyName = getChannelChainPropertyString(stringBuilder,
//                            FlumePropertyConstant.Source.SOURCES, name);
//                } else {
//                    propertyName = getChannelChainPropertyString(stringBuilder,
//                            FlumePropertyConstant.Sink.SINKS, name);
//                }
//
//                List<FlumeChannel> components = (List) field.get(component);
//                for (int i = 0; i < components.size() - 1; i++) {
//                    stringBuilder.append(components.get(i).name()).append(",");
//                }
//                stringBuilder.append(components.get(components.size() - 1).name());
//                properties.put(propertyName, stringBuilder.toString());
//            }
//        }
//    }
//
//
//    /**
//     * 获取FlumeComponent的类型
//     *
//     * @param component
//     * @return
//     */
//    private String getFlumeComponentType(FlumeComponent component) {
//        Class clazz = component.getClass();
//        if (clazz == FlumeComponent.class || clazz == Object.class) {
//            throw new IllegalArgumentException("无法解析FlumeComponent及其之上的类");
//        }
//        Class superClass = clazz.getSuperclass();
//        while (superClass != FlumeComponent.class) {
//            clazz = superClass;
//            superClass = superClass.getSuperclass();
//        }
//        return ((FlumeComponentType) clazz.getAnnotation(FlumeComponentType.class)).value();
//    }
//
//    /**
//     * 获取相关Flume组件的channel链配置名
//     *
//     * @param stringBuilder
//     * @param componentType
//     * @param componentName
//     * @return
//     */
//    private String getChannelChainPropertyString(StringBuilder stringBuilder, String componentType, String componentName) {
//        String propertyName = stringBuilder.append(agent.agentName()).append(".")
//                .append(componentType).append(".").append(componentName).append(".")
//                .append(FlumePropertyConstant.Channel.CHANNELS).toString();
//        stringBuilder.delete(0, stringBuilder.length());
//        return propertyName;
//    }
//
//
////    @Override
////    public boolean generateSourceProperties(FlumeSource source) {
//////        Class
////        return false;
////    }
////
////    @Override
////    public boolean generateChannelProperties(FlumeChannel channel) {
////        return false;
////    }
////
////    @Override
////    public boolean generateSinkProperties(FlumeSink sink) {
////        return false;
////    }
//
//    @Override
//    public FlumeAgent agent() {
//        return this.agent;
//    }
//
//    @Override
//    public Properties agentProperty() {
//        return properties;
//    }
//}
